<template>
    <div class="main">
        <el-form ref="form" class="form" :model="form">
            <el-form-item label="用户名">
                <el-input v-model="form.username"/>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="form.password"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {storage} from "@/utils/common"

    export default {
        name: "login",
        data() {
            return {
                form: {
                    username: "admin",
                    password: "123456"
                }
            }
        },
        methods: {
            async login() {
                let data = [
                    {
                        name: "绘制",
                        path: "/main/index",
                        componentPath: "/main/index",
                        meta: {
                            icon: "el-icon-s-home",
                        }
                    },
                    {
                        name: "路由菜单",
                        path: "/menu/index",
                        componentPath: "/menu/index",
                        meta: {
                            icon: "el-icon-menu"
                        },
                        children: [
                            {
                                name: "菜单列表",
                                path: "/menu/index",
                                componentPath: "/menu/index",
                                meta: {
                                    icon: "el-icon-s-home",
                                }
                            },
                            {
                                name: "添加菜单",
                                path: "/menu/index/add",
                                componentPath: "/menu/add",
                                meta: {
                                    icon: "el-icon-s-home",
                                }

                            }
                        ]
                    }
                ]
                this.$openLoading()

                this.$openLoading()
                /*let res = await this.$api.post("/auth/login", this.form).catch(e => {
                    this.$message.error(e.msg)
                })*/
                let res
                if(this.form.username === "admin" && this.form.password === "123456"){
                  res = {
                    code: 200,
                    msg:"登录成功",
                    data:{
                      token:"123123123",
                    }
                  }
                }  else {
                  this.$message.error("用户名或密码错误")
                }
                this.$closeLoading()
                if (!res) return
                if (res.code === 200) {
                    await this.$store.dispatch("generateRoutes", data)
                        .catch(e => console.log(e))
                    storage(true).set("token", res.data.token)
                    storage(true).set("userInfo", res.data)
                    this.$router.push("/")
                } else {
                    this.$message.error(res.msg)
                }

            }
        }
    }
</script>

<style scoped lang="scss">
    .main {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;

        .form {
            width: 300px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
            padding: 30px;
        }
    }
</style>
